package com.yungu.swift.api.route.v2.passenger;


import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.facade.PassengerFacade;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.order.model.param.ApiValuationParam;
import com.yungu.swift.order.model.vo.OrderVo;
import com.yungu.swift.order.service.OrderFareService;
import com.yungu.swift.order.service.OrderService;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController(value = "orderApiV2")
@RequestMapping(value = "/api/v2/passenger/order")
@Api(value = "【乘客】【订单】【公用】乘客订单通用接口", tags = "乘客订单相关操作")
public class OrderApi extends BaseApi {

    @Autowired
    private PassengerFacade passengerFacade;
    @Reference
    private OrderService orderService;
    @Reference
    private OrderFareService orderFareService;

    @RequestMapping(value = {"/create"}, method = RequestMethod.POST)
    @ApiOperation(value = "创建订单接口", notes = "create", response = ResponseData.class)
    public ResponseData create(@RequestHeader String appid, @RequestBody OrderVo orderVo, HttpServletRequest request) {
        String passengerUuid = getUserUid(request);
        orderVo.setPassengerUuid(passengerUuid);
        orderVo.setAppid(appid);
        orderVo.setPassengerIp(getUserIP(request));
        orderVo.setPassengerPort(request.getRemotePort());
        return passengerFacade.createOrder(orderVo);
    }

    @RequestMapping(value = {"/valuation"}, method = RequestMethod.POST)
    @ApiOperation(value = "订单估价接口", notes = "create", response = ResponseData.class)
    public ResponseData<Map<String, Object>> valuation(@RequestHeader String appid, @RequestBody ApiValuationParam valuationParam, HttpServletRequest request) {
        valuationParam.setUserUuid(getUserUid(request));
        valuationParam.setAppid(appid);
        try {
            return orderFareService.commonValuation(valuationParam);
        } catch (Exception e) {
            log.error("passengerUuid:{}, appid:{} 估价失败", getUserUid(request), appid, e);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "估价失败");
    }

    @RequestMapping(value = {"/getNoEvaluation"}, method = RequestMethod.POST)
    @ApiOperation(value = "获取未评价订单", notes = "create", response = ResponseData.class)
    public ResponseData<Map<String, Object>> getNoEvaluationOrder(HttpServletRequest request) {
        String passengerUuid = getUserUid(request);
        if (StringUtils.isEmpty(passengerUuid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "未找到乘客信息");
        }
        return orderService.getNoEvaluationOrder(passengerUuid);
    }
}
